package com.woniuxy01;


import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.security.cert.CertPath;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 作者
 * @create 2025-06-17 9:48
 */
public class Test01 {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchFieldException, NoSuchMethodException, InvocationTargetException {
        Map<String, Object> map = new HashMap<>();
        map.put("bid", 1);
        map.put("bname", "zs");
        map.put("age", 11);
        map.put("test", 11);

        System.out.println(mapToBean(map, Book.class));

//        Student student = mapToBean(map, Student.class);
//
//        System.out.println(student);

    }

    private static <T> T  mapToBean(Map<String, Object> map,Class<T> clazz ) throws InstantiationException, IllegalAccessException {
        T obj = (T) clazz.newInstance();
        for (String key : map.keySet()){
            try {
                Field field = clazz.getDeclaredField(key);
                field.setAccessible(true);
                field.set(obj, map.get(key));
            } catch (Exception e) {
//                throw new RuntimeException(e);
            }
        }
        return obj;
    }


    private static void test09() throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        Class clazz = Class.forName("com.woniuxy01.Student");

        Constructor constructor1 = clazz.getConstructor();
        Student student2 = (Student) constructor1.newInstance();
        System.out.println(student2);

        Constructor constructor2 = clazz.getConstructor(Integer.class, String.class,
                Integer.class, String.class, String.class);
        Student student3 = (Student) constructor2.newInstance(1, "zs", 18, "123@qq.com", "123456");
        System.out.println(student3);
    }

    private static void test08() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        Class clazz = Class.forName("com.woniuxy01.Student");
        Student student1 = (Student) clazz.newInstance();
        System.out.println(student1);
        //获取public构造方法
        Constructor[] constructors = clazz.getConstructors();
        for (Constructor constructor : constructors) {
            System.out.println(constructor);
        }
    }

    private static void test07() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Class clazz = Class.forName("com.woniuxy01.Student");
        Student student = (Student) clazz.newInstance();
        Method studyMethod1 = clazz.getMethod("study");
        studyMethod1.invoke(student);
        Method studyMethod2 = clazz.getMethod("study", String.class);
        studyMethod2.invoke(student, "java");
        Method studyMethod3 = clazz.getMethod("study", String.class, int.class);
        studyMethod3.invoke(student, "java", 5);
    }

    private static void test06() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Class clazz = Class.forName("com.woniuxy01.Student");
        Student student = (Student) clazz.newInstance();
        Method studyMethod = clazz.getMethod("study");
        //student.study();
        studyMethod.invoke(student);

        Method sleepMethod = clazz.getDeclaredMethod("sleep");
        sleepMethod.setAccessible(true);
        sleepMethod.invoke(student);
    }

    private static void test05() throws ClassNotFoundException {
        Class clazz = Class.forName("com.woniuxy01.Student");
        Method[] methods = clazz.getMethods();
        for (Method method : methods) {
            System.out.println(method);
        }
        System.out.println("--------------");
        Method[] declaredMethods = clazz.getDeclaredMethods();
        for (Method method : declaredMethods) {
            System.out.println(method);
        }
    }

    private static void test04() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchFieldException {
        Class clazz = Class.forName("com.woniuxy01.Student");
        Student stu = (Student) clazz.newInstance();
        System.out.println(stu);
        Field emailField = clazz.getField("email");
        //stu.getEmail();
        //stu1.getEmail();
        //获取属性的字段
        System.out.println(emailField.get(stu));
        //给属性赋值  stu.setEmail("xxxx")
        emailField.set(stu, "123@qq.com");
        System.out.println(emailField.get(stu));

        Field snameField = clazz.getDeclaredField("sname");
        //暴力访问
        snameField.setAccessible(true);
        snameField.set(stu, "张三");
        System.out.println(stu);
    }

    private static void test03() throws ClassNotFoundException, NoSuchFieldException {
        Class clazz = Class.forName("com.woniuxy01.Student");
        //getFields()，获取public属性
        Field[] fields = clazz.getFields();
        for (Field field : fields) {
            System.out.println(field);
        }
        System.out.println("---------------");
        //获取所有属性
        Field[] declaredFields = clazz.getDeclaredFields();
        for (Field field : declaredFields) {
            System.out.println(field);
        }
        System.out.println("---------------");
        //获取指定的public属性
        Field emailField = clazz.getField("email");
        System.out.println(emailField);
        System.out.println("---------------");
        //获取指定的属性
        Field snameField = clazz.getDeclaredField("sname");
        System.out.println(snameField);
    }

    private static void test02() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
//        Class clazz = Class.forName("com.woniuxy01.Student");
//        //创建对象,底层使用无参构造
//        Aaa aaa = (Aaa) clazz.newInstance();
////        System.out.println(student);
    }

    private static void test01() throws ClassNotFoundException {
        //创建Class类型的对象:获取二进制字节码对象
//        Class  clazz1= Student.class;
//
//        Student student=new Student();
//        Class clazz2=student.getClass();
//        //框架中使用这种，这种最灵活，可以通过修改配置文件，程序读取，
//        Class clazz3 = Class.forName("com.woniuxy01.Student");
//
//        System.out.println(clazz1==clazz2);
//        System.out.println(clazz3==clazz2);
    }
}
